const orderFacade = require('../../app/facade/order.facade.js'); 
const _  =  require('lodash');

module.exports = {
    addOrder: addOrder,
    updateOstateByOid:updateOstateByOid,
    findOrderByClient:findOrderByClient,
    findOrderByOid:findOrderByOid,
    findOrderByOstateandClient:findOrderByOstateandClient
}

function addOrder(req, res, next){
    console.log('addorder come in');
    let order = req.body;
    order.otime = new Date();
    orderFacade.addOrder(order,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }        
    });
}

function findOrderByClient(req, res, next) {
    let cid = req.params.cid;
    orderFacade.findOrderByClient(cid, (error, result)=>{
        if(error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });
}

function findOrderByOid(req, res, next) {
    let oid = req.params.oid;
    orderFacade.findOrderByOid(oid, (error, result)=>{
        if(error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    })
}

function findOrderByOstateandClient(req, res, next) {
    let cid = req.params.cid;
    let ostate = req.params.ostate;
    orderFacade.findOrderByOstateandClient(cid, ostate, (error, result)=>{
        if(error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });
}

function updateOstateByOid(req, res, next) {
    let oid=req.params.oid;
    let oldstate=req.params.oldstate;
    orderFacade.updateOstateByOid(oid, oldstate, (error,result)=>{
        if(error) {
            return next(new Error(error));
        } else {
            console.log('db controller...');
            console.log(result);
            return res.json(result);
        }
    });
}

